package com.ztesoft.sca.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.qualitycheck.model.v20160801.SaveReviewResultRequest;
import com.aliyuncs.qualitycheck.model.v20160801.SaveReviewResultResponse;
import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.common.SysConstant;
import com.ztesoft.sca.util.exception.ExceptionUtil;
import com.ztesoft.sca.util.message.ResponseUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
public class TaskResultController {
	private static final Logger logger = Logger.getLogger(AiChatAudioController.class);
	
	/*
	 * 质检复核  手动复核
	 * */
	@RequestMapping(value = "/aichat/SCAreviewTaskService",method = {RequestMethod.POST})
	public String reviewService(@RequestBody String requestBody,HttpServletRequest request) {
		JSONObject json = JSONObject.fromObject(requestBody);
		if(json == null){
			return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,Constants.INF_DESC_NULL_REQUESTBODY);
		}
		
        String taskId=json.getString("taskId");
        String vid = json.getString("vid"); //vid=1000=fileId
        
        JSONArray jsonarr= json.getJSONArray("reviewInfoList");
        String reviewInfoList="";
        for (int i = 0; i < jsonarr.size(); i++) {  
	        String reviewInfoListStr="{\n" +
	                "\"hitId\":\""+jsonarr.getJSONObject(i).getString("hitId")+"\",\n" + //质检语音命中id
	                "\"reviewResult\":\""+jsonarr.getJSONObject(i).getString("reviewResult")+"\",\n" +
	                "\"rid\":\""+jsonarr.getJSONObject(i).getString("rid")+"\"\n" +
	                "}\n";
	          reviewInfoList=reviewInfoList+","+reviewInfoListStr;
        }
        if(!"".equals(reviewInfoList)){
        	reviewInfoList=reviewInfoList.substring(1);
        }
        
        JSONArray jsonScore = json.getJSONArray("handScoreIdList");
        List<Long> handScoreIdList=new ArrayList<>();
        for (int i = 0; i < jsonScore.size(); i++) {  
        	handScoreIdList.add(jsonScore.getLong(i));  
        } 

		String resultResponse="";
		try{
			IClientProfile profile = DefaultProfile.getProfile(SysConstant.REGION_ID, SysConstant.ACCESS_KEY_ID, SysConstant.ACCESS_KEY_SECRET);
			DefaultProfile.addEndpoint(SysConstant.ENDPOINT_NAME, SysConstant.REGION_ID, SysConstant.PRODUCT, SysConstant.DOMAIN);
			IAcsClient client = new DefaultAcsClient(profile);
	        SaveReviewResultRequest saveReviewResultRequest = new SaveReviewResultRequest();
	        saveReviewResultRequest.setAcceptFormat(FormatType.JSON);
	        String jsonStrSave = "\n{" +
	                "    \"handScoreIdList\":" +handScoreIdList +",\n" +
	                "    \"reviewInfoList\":[\n" +reviewInfoList+
	                "    ],\n" +
	                "    \"taskId\":\""+taskId+"\",\n" +
	                "    \"vid\":\""+vid+"\"\n" +
	                "}";
	        saveReviewResultRequest.setJsonStr(jsonStrSave);
	        SaveReviewResultResponse responseSave = client.getAcsResponse(saveReviewResultRequest);
	        resultResponse = JSON.toJSONString(responseSave);
	        logger.info("----------reviewService---手动复核-------"+resultResponse);
		}catch (ServerException e) {
			return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		} catch (ClientException e) {
			return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
		return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,"质检复核成功的任务taskId:"+taskId);
	}
}
